#ifndef ELDRAWER_H
#define ELDRAWER_H

#include "../basic/elcomponent.h"

class ElTextButton;
class QPropertyAnimation;
class ElDrawer;

// it's used to display main content of the drawer, but only can ElDrawer be accessed to it.
class ElDrawerPrivate : public ElComponent
{
    Q_OBJECT
    friend class ElDrawer;

private:
    static const int Padding_Lr;
    static const int Padding_Tb;

    ElDrawerPrivate(ElDrawer *parent);

    void paintEvent(QPaintEvent *e) override;
    void showEvent(QShowEvent *e) override;
    void hideEvent(QHideEvent *e) override;
    void initPalette() override;

    QString _title;
    QWidget *_content = nullptr;

    QPropertyAnimation *_animOpa = nullptr;
    QPropertyAnimation *_animPos = nullptr;
    ElTextButton *_closeBtn = nullptr;

    void createCloseBtn(ElDrawer *parent);
    void initAnimation();
    void updatePosAnimation();
    std::pair<QPoint, QPoint> calcPos();
    void fadeOut();
};

class ElDrawer : public ElComponent
{
    Q_OBJECT
public:
    enum CloseBehavior { Delete, KeepOn };
    enum Direction { Rtl, Ltr, Ttb, Btt };

    ElDrawer(QWidget *parent);

    CloseBehavior closeBehavior() const;
    void setCloseBehavior(CloseBehavior closeBehavior);

    QString title() const;
    void setTitle(const QString &title);

    QWidget *content() const;
    void setContent(QWidget *content);

    qreal drawerSize(Qt::SizeMode sizeMode = Qt::RelativeSize) const;
    void setDrawerSize(qreal sz, Qt::SizeMode sizeMode = Qt::RelativeSize);

    Direction direction() const;
    void setDirection(Direction direction);

    QColor maskColor() const;
    void setMaskColor(const QColor &maskColor);

    bool hasModal() const;
    void setModal(bool modal);

    bool isShowClose() const;
    void setShowClose(bool showClose);

    bool isCloseOnPressEsc() const;
    void setCloseOnPressEsc(bool closeOnPressEsc);

    bool isMaskClosable() const;
    void setMaskClosable(bool maskClosable);

signals:
    void closedBefore();

    void modalChanged();

public slots:
    void fadeOut();
    void adjustfySize();

protected:
    void paintEvent(QPaintEvent *e) override;
    void showEvent(QShowEvent *e) override;
    void mouseReleaseEvent(QMouseEvent *e) override;
    void keyPressEvent(QKeyEvent *e) override;
    void initPalette() override;

private:
    Q_PROPERTY(CloseBehavior closeBehavior READ closeBehavior WRITE setCloseBehavior)
    Q_PROPERTY(QString title READ title WRITE setTitle)
    Q_PROPERTY(Direction direction READ direction WRITE setDirection)
    Q_PROPERTY(QColor maskColor READ maskColor WRITE setMaskColor)
    Q_PROPERTY(bool modal READ hasModal WRITE setModal)
    Q_PROPERTY(bool showClose READ isShowClose WRITE setShowClose)
    Q_PROPERTY(bool closeOnPressEsc READ isCloseOnPressEsc WRITE setCloseOnPressEsc)
    Q_PROPERTY(bool maskClosable READ isMaskClosable WRITE setMaskClosable)

    ElDrawerPrivate *_d = nullptr;
    CloseBehavior _closeBehavior = KeepOn;
    Direction _direction = Rtl;
    QColor _mask = Qt::black;
    bool _modal = true;
    bool _showClose = true;
    bool _closeOnPressEsc = true;
    bool _maskClosable = true;
    qreal _percentSize = 30;
    Qt::SizeMode _sizeMode = Qt::RelativeSize;
};

#endif // ELDRAWER_H
